/*
 * UserValidatorSocket.java
 *
 * Created on 19 f�vrier 2008, 12:18
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.great.functionalCore;

import br.great.beans.User;
import br.great.exception.*;
import java.net.*; 
import java.io.*;
import br.great.config.*;

/**
 *
 * @author defautce
 */
public class UserValidatorSocket {
    private User user;
    
    private static UserValidatorSocket instance;
    
    private UserValidatorSocket(){
    }
    
    public static UserValidatorSocket getInstance(){
        if(instance == null){
            instance = new UserValidatorSocket();
        }
        return instance;
    }
    
    public User validateUser(String login, String password) throws UnknownUserException {
        try {
            Socket client = new Socket(Config.address, Config.port);
            PrintWriter out = new PrintWriter(client.getOutputStream(),true);
            BufferedReader in = new BufferedReader (new InputStreamReader(client.getInputStream()));
            String request = "USER"+login+"#"+password;
            out.println(request);
            while(true){
                String response = in.readLine();
                if((response!=null) && (!response.equals("false"))){
                    User u = new User();
                    u.setLogin(login);
                    u.setPassword(password);
                    u.setURLFOAFProfil(response);
                    return u;
                }
            }
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
}
